package com.sdehua.wear.controller;

import com.sdehua.wear.entity.Collect;
import com.sdehua.wear.entity.CollectVO;
import com.sdehua.wear.service.CollectService;
import com.sdehua.wear.utils.ResultEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Collect)表控制层
 *
 * @author makejava
 * @since 2021-12-31 15:28:27
 */
@RestController
@RequestMapping("collect")
@CrossOrigin
public class CollectController {
    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    @GetMapping("queryOne")
    public ResultEntity selectOne(String userId,String goodsId) {
        Collect collect = this.collectService.queryOne(userId,goodsId);
        if (collect==null){
            return ResultEntity.fail("未收藏该服饰");
        }
        return ResultEntity.success(collect);
    }

    @RequestMapping("setFavorite")
    public ResultEntity setFavorite(String userId,String goodsId){
        Collect collect = collectService.setFavorite(userId,goodsId);
        if (Objects.equals(collect,null)){
            return ResultEntity.fail("收藏失败");
        }else if (Objects.equals(collect.getUserId(),-1) && collect.getGoodsId() == "-1"){
            return ResultEntity.fail("已收藏");
        }
        return ResultEntity.success(collect);
    }

    @RequestMapping("deleteFavorite")
    public ResultEntity deleteFavorite(String userId,String goodsId){
        boolean flag = collectService.deleteFavorite(userId, goodsId);
        if (!flag){
            return ResultEntity.fail("取消失败");
        }
        return ResultEntity.success("取消收藏");
    }

    @RequestMapping("queryAll")
    public ResultEntity queryAll(String userId){
        List<CollectVO> list = collectService.queryAll(userId);
        if (Objects.equals(list,null)){
            return ResultEntity.fail("查询失败");
        }
        return ResultEntity.success(list);
    }

}